package com.lh.tams.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.lh.tams.controller.qo.CaseQo;
import com.lh.tams.domain.TamsCase;
import com.lh.tams.service.CaseService;
import com.lh.tams.util.DataGridModel;
import com.lh.tams.util.DateUtil;
import com.lh.tams.util.ExcelUtil;
import com.lh.tams.util.Page;
import com.lh.tams.util.Status;

/**
 * 导出Excel
 * @author hc24
 *
 */
@Controller
@RequestMapping("/download")
public class DownloadController {
	private CaseService caseService;
	
	public void setCaseService(CaseService caseService) {
		this.caseService = caseService;
	}

	@RequestMapping("/downCase.adm")
	public void downloadCase(HttpServletResponse response, CaseQo caseQo,String[] headers,Integer[] columnView){
		DataGridModel dgm=new DataGridModel();
		dgm.setPage(1);
		dgm.setRows(Integer.MAX_VALUE);
		Page page=caseService.queryForCase(dgm, caseQo);
		List<TamsCase> list=page.getRows();
		
		//导出Excel
		String title="案件查询结果";
		String filename="";
		try {
			filename=new String(title.getBytes("gb2312"), "iso-8859-1");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		List<String> headersList=new ArrayList<String>();		//表头
		List<Integer> columnViewList=new ArrayList<Integer>();  //列宽
		List<Object[]> datas=new ArrayList<Object[]>();
		for(int i=0;i<headers.length;i++){
			headersList.add(headers[i]);
			columnViewList.add(columnView[i]/10);
		}
		
		for(int i=0;i<list.size();i++){
			TamsCase tcase=list.get(i);
			Object[] obj=new Object[headers.length];
			obj[0]=DateUtil.format(tcase.getAlarmDate(), "yyyy-MM-dd HH:mm:ss");
			obj[1]=tcase.getCaseAddress();
			obj[2]=tcase.getTamsDeptByOutDept()==null?"暂未指定":tcase.getTamsDeptByOutDept().getDeptName();
			obj[3]=tcase.getTamsPersonByOutPerson()==null?"暂未指定":tcase.getTamsPersonByOutPerson().getPersonName();
			obj[4]=tcase.getAlarmType();
			datas.add(obj);
		}
		
		//response.setCharacterEncoding("gbk");
		response.setContentType("application/vnd.ms-excel");     
        response.setHeader("Content-disposition", "attachment;filename="+filename ); 
		InputStream is=null;
		OutputStream os=null;
		try {
		 	 is=ExcelUtil.exportExcel(title, headersList, columnViewList, datas);
		 	 os=response.getOutputStream();  
	            byte[] b=new byte[1024];  
	            int length;  
	            while((length=is.read(b))>0){  
	                os.write(b,0,length);  
	            }  
	            is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				is.close();
				os.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
		
		
	}
	
}
